import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Welcome from '../components/welcome.vue'
import Users from '../components/users/Users.vue'
import Rights from '../components/rights/rights.vue'
import Roles from '../components/rights/roles.vue'
import Cate from '../components/goods/cate.vue'
import Params from '../components/goods/Params.vue'
import Goods from '@/components/goods/goods'
import Addgoods from '@/components/goods/addgoods'
import Order from '@/components/Order/Order'
import Reports from '@/components/reports/Reports'

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: Goods },
        { path: '/goods/add', component: Addgoods },
        { path: '/orders', component: Order },
        { path: '/reports', component: Reports }
      ]
    }
  ]
})

router.beforeEach((to, before, next) => {
  if (to.path === '/login') return next()
  // 获取token
  const token = window.sessionStorage.getItem('token')

  if (!token) return next('/login')
  next()
})

export default router
